import { ForwardRefRenderFunction, memo, useImperativeHandle, forwardRef } from 'react'
import { message } from '@hs-admin/utils'
import { useSearchParams } from 'react-router-dom'
import { Space, Button } from 'antd'
import { useTypeStore } from '@/store/useBasicDataItem'
import { onlineDataItem } from '@/api/assetMaintain/basic/item'
import { HsAdminDataItemExample } from '@hs-admin/components'

interface Props {
  back: () => void
}

const Step3: ForwardRefRenderFunction<unknown, Props> = (props: Props, ref: any) => {
  const { back } = props
  const [searchParams] = useSearchParams()
  let id: string = searchParams.get('id') || ''
  const data_item_id = useTypeStore((state) => state.data_item_id)
  if (!id && data_item_id) {
    id = data_item_id
  }

  const setTabKey = useTypeStore((state) => state.setTabKey)
  const status = useTypeStore((state) => state.status)

  const goStep2 = () => {
    setTabKey('2')
  }

  const finish = async () => {
    await onlineDataItem({ data_product_id: id })
    message.success('上架成功')
    back()
  }

  useImperativeHandle(
    ref,
    () => {
      return {}
    },
    []
  )

  return (
    <div>
      <HsAdminDataItemExample dataItemId={id} showRefresh />
      <Space className="flex justify-end">
        <Button type="primary" onClick={goStep2}>
          上一步
        </Button>
        <Button onClick={back}>取消</Button>
        {status !== 'published' && (
          <Button type="primary" onClick={finish}>
            提交上架审批
          </Button>
        )}
      </Space>
    </div>
  )
}

export default memo(forwardRef(Step3))
